package com.manager;

import android.util.Log;

import com.element.character.player.Player;
import com.game.activity.MainActivity;

import world.World;

public class GameManager {
	private World gameWorld;
	
	/* used to communicate with the ViewManager */
	private ReturnState charGenState = ReturnState.NEW_SERVICE;
	
	/* used to crash the game when vmThread does not exist when it should */
	private static final int SLEEPY = 1;
	private int timeOutCount = 0;
	private int TIMEOUT_TIME = 2000;
	
	/* internal stuff */
	private boolean pause = true;
	private boolean init = true;
	private boolean createNewWorld = false;
	
	private long tick = 0;
	
	/* constants */
	private static final String TAG = "GameManager";
	
	/* main tick method -- use this to update the game */
	public void tick(){
		
		if(init){
			init();
		} else if(!pause){
			gameWorld.update();
			tick++;
			Log.d(TAG, "Tick: " + tick);
		} else {
			Log.d(TAG, "Tick (game is paused): " + tick);
		}
	}
	
	/* initializes the game world -- communicates with vmThread and the "GUI" */
	public void init(){
		charGenState = ReturnState.WAIT;
		
		/* if vmThread does not exist or if it is not sleeping then wait for it to be created */
		while(MainActivity.vmThread == null || MainActivity.vmThread.getState() != Thread.State.WAITING){
			try {
				/*  
				String s = "timeOutCount (break at " + TIMEOUT_TIME + ") = " + timeOutCount;
				Log.d(TAG, s); */
				
				Thread.currentThread().sleep(SLEEPY);
				timeOutCount += SLEEPY;
				if(timeOutCount >= TIMEOUT_TIME){
					/* -10 represents TIMEOUT */
					Log.d(TAG, "Error: Timeout reached! Communication with ViewManager has failed.");
					System.exit(-10);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		timeOutCount = 0;
		
		/* check for existing characters/world */
		if(World.worldExists()){
			/* there exists a world! load it! */
			Log.d(TAG, "World exists! GameManager loading world...");
			charGenState = ReturnState.PLAYER_EXISTS;
			gameWorld = World.loadExistingWorld();
			Log.d(TAG, "World loaded!");
		} else {
			/* no world exists :( tell the MainActivity/ViewManager so it can start chargen */
			Log.d(TAG, "No existing world!");
			createNewWorld = true;
			charGenState = ReturnState.PLAYER_DOES_NOT_EXIST;
		}
		
		Log.d(TAG, "Return state set! Waking up ViewManager..");
		
		synchronized(MainActivity.managerSynchLock) {
			MainActivity.managerSynchLock.notify();
		}
		
		if(createNewWorld){
			gameWorld = World.createNewWorld();
		}	
		synchronized(MainActivity.managerSynchLock){
			if(MainActivity.vmThread.getState() == Thread.State.WAITING){
				/* vmThread finished waiting */
				MainActivity.managerSynchLock.notify();
			} else {
				try {
					MainActivity.managerSynchLock.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		Log.d(TAG, "Unpausing game. *Ticks* should now start.");
		pause = false;
		init = false;
	}
	
	public void updatePlayer(){
		/* what does the player want to do? simulate him here */
	}
	
	public void updateWorld(){
		/* what happens in the world? simulate world here */
	}
	
	public GameManager(){
		/* nothing special here */
	}
	
	public World getWorld(){
		return gameWorld;
	}
	
	public ReturnState getCharGenState(){
		return charGenState;
	}
	
	public void setPlayer(Player p){
		gameWorld.player = p;
	}
	
	public void setCharGenState(ReturnState s){
		charGenState = s;
	}
}
